package com.rssreader.client.components;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Popup;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rssreader.client.RSSReaderConstants;
import com.rssreader.client.mvc.events.AppEvents;
import com.rssreader.client.services.FeedServiceAsync;

public class LinkFeedPopup extends Popup {
    // for the user to enter the URL
    private final TextField<String> tfUrl = new TextField<String>();

    public LinkFeedPopup() {
	setSize(300, 55);
	setBorders(true);
	setShadow(true);
	setAutoHide(false);
    }

    @Override
    // Subclasses must override and ensure setElement is called for lazy
    // rendered components.
    // the target element, the insert location
    protected void onRender(Element parent, int pos) {
	super.onRender(parent, pos);
	// tell the user to enter an URL in the text field
	final Text txtExplaination = new Text("Enter a feed url");

	// submit the value of the URL field
	final Button btnAdd = new Button("add");

	// respond to the user clicking on the button
	btnAdd.addSelectionListener(new SelectionListener<ButtonEvent>() {
	    @Override
	    public void componentSelected(ButtonEvent ce) {
		// deal with the value the user enters
		addFeed(tfUrl.getValue());
	    }
	});

	tfUrl.setAllowBlank(false);
	tfUrl.setRegex("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$");
	tfUrl.setAllowBlank(false);
	tfUrl.getMessages().setBlankText("Please enter the URL of an existing feed");
	tfUrl.setAutoValidate(true);
	tfUrl.getMessages().setRegexText(
		"The link field must be a URL e.g. http://www.example.com/rss.xml");

	tfUrl.addKeyListener(new KeyListener() {
	    public void componentKeyDown(ComponentEvent event) {
		if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
		    addFeed(tfUrl.getValue()); // deal with the value the user
					       // enters
		}
	    }
	});

	final BorderLayout layout = new BorderLayout();
	setLayout(layout); // laying out the popup

	final BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
		20);
	northData.setMargins(new Margins(2));
	add(txtExplaination, northData);

	final BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
	centerData.setMargins(new Margins(2));
	add(tfUrl, centerData);

	final BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST, 50);
	eastData.setMargins(new Margins(2, 2, 2, 20));
	add(btnAdd, eastData);

    }

    public void addFeed(final String feedUrl) {
	final FeedServiceAsync feedService = Registry
		.get(RSSReaderConstants.FEED_SERVICE);
	feedService.addExistingFeed(feedUrl, new AsyncCallback<Void>() {

	    @Override
	    public void onSuccess(Void result) {
		// clear the URL TextField
		tfUrl.clear();
		Info.display("RSS Reader", "Feed at " + feedUrl
			+ "added successfully");
		Dispatcher.forwardEvent(AppEvents.FeedAdded);
		// hide the popup
		hide();
	    }

	    @Override
	    public void onFailure(Throwable caught) {
		Info.display("RSS Reader", "Failed to add feed at: " + feedUrl);
	    }
	});
	// Window.alert(url); //彈出message
    }

}
